package edu.zut.controller;

import edu.zut.service.UserService;
import edu.zut.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet", value = "/user/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String user_name = request.getParameter("user_name").trim();
        String user_psw = request.getParameter("user_psw").trim();
        //验证用户名和密码是否为空
        //验证数据库是否存在  调用业务层操作
        UserService userService = new UserServiceImpl();
        boolean result = userService.userLogin(user_name, user_psw);
        HttpSession session = request.getSession();
        if (result){
            //验证通过,跳转到查询员工信息请求，将信息查询后动态的展示
            session.setAttribute("user_name",user_name);
            session.setAttribute("loginStatus","true");
            response.sendRedirect(request.getContextPath() + "/emp/showPage");
        }else {
            request.setAttribute("error","用户名或密码错误");
            request.getRequestDispatcher("/view/login.jsp").forward(request,response);
        }


    }
}
